package com.cloud.tsp.controller;

import com.cloud.api.constants.StrVoPage;
import com.cloud.core.exception.MyException;
import com.cloud.core.util.R;

import com.cloud.api.domain.stats.QueryRecord;
import com.cloud.api.domain.stats.RecordList;
import com.cloud.api.domain.tsp.*;
import com.cloud.api.validgroup.ValidateGroupSelectVin;
import com.cloud.api.validgroup.ValidateGroupUpdate;
import com.cloud.api.constants.StrVo;
import com.cloud.log.annotation.Log;
import com.cloud.log.enums.BusinessType;
import com.cloud.log.enums.OperatorType;
import com.cloud.mybatis.domain.TableDataInfo;
import com.cloud.tsp.config.MysqlConfig;
import com.cloud.tsp.service.IVehicleDataService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;

import java.math.BigDecimal;
import java.util.Map;

/**
 * <p>
 * OLAP 前端控制器
 * </p>
 *
 * @author haohaibo
 * @since 2023-12-01
 */
@RestController
@RequestMapping("/vehicle-data")
public class VehicleDataController {

    @Resource
    private IVehicleDataService vehicleDataService;
    @Autowired
    private MysqlConfig mysqlConfig;

    @Tag(name = "v1.0")
    @Operation(summary = "时间段内的车辆轨迹纠偏（管理）")
    @PostMapping("/getTrackByVinTime")
    @Log(title = "实时监控-时间段内的车辆轨迹纠偏", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public TableDataInfo<VehicleLocation> selectTrackByVinTime(@RequestBody @Valid VinTimePeriod para){
        return vehicleDataService.selectTrackByVinTime(para);
    }

    @Tag(name = "v1.0")
    @Operation(summary = "时间段内的车辆轨迹无纠偏（管理）")
    @PostMapping("/getTrackByVinTimeNoCorrect")
    @Log(title = "实时监控-时间段内的车辆轨迹无纠偏", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public TableDataInfo<VehicleLocation> selectTrackByVinTimeNoCorrect(@RequestBody @Valid StrVoPage para){
        return vehicleDataService.selectTrackByVinTimeNoCorrect(para);
    }

    @Tag(name = "v1.0")
    @Operation(summary = "通过vin id查询CAN信息（管理）")
    @PostMapping("/getVehicleDataByVin")
    @Log(title = "实时监控-通过vin查询CAN信息", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public R<VehicleDataVo> selectVehicleDataByVin(@RequestBody @Valid StrVo para){
        return R.ok(vehicleDataService.selectVehicleDataByVin(para.getId()));
    }

    @Tag(name = "v1.0")
    @Operation(summary = "翻译地址")
    @PostMapping("/translateAddress")
    @Log(title = "实时监控-翻译地址", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public R<String> translateAddress(@RequestBody Map<String,String> map){
        String lat = map.get("lat");
        String lng = map.get("lng");
        if (lng == null || lat == null){
            throw new MyException("validate.address.empty");
        }
        return R.ok(vehicleDataService.getLocation(new BigDecimal(lat), new BigDecimal(lng)));
    }

    @Tag(name = "v1.0")
    @Operation(summary = "查询报文")
    @PostMapping("/getRecord")
    @Log(title = "报文管理-查询报文", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public TableDataInfo<RecordList> selectRecord(@Validated(ValidateGroupSelectVin.class) @RequestBody @Valid QueryRecord para){
        para.setDataSource(mysqlConfig.getDataSource());
        return vehicleDataService.selectRecord(para);
    }

    @Tag(name = "v1.0")
    @Operation(summary = "查询实时、补发报文详情")
    @PostMapping("/getRecordDetail")
    @Log(title = "报文管理-查询实时、补发报文详情", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public R<VehicleDataVo> selectRecordDetail(@Validated(ValidateGroupUpdate.class) @RequestBody @Valid QueryRecord para){
        return R.ok(vehicleDataService.selectRecordDetail(para));
    }
}